<template>
  <el-dialog
    :title="!dataForm.inquiryId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="患者id" prop="patientId">
      <el-input v-model="dataForm.patientId" placeholder="患者id"></el-input>
    </el-form-item>
    <el-form-item label="问诊日期" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="问诊日期"></el-input>
    </el-form-item>
    <el-form-item label="患者姓名" prop="patientName">
      <el-input v-model="dataForm.patientName" placeholder="患者姓名"></el-input>
    </el-form-item>
    <el-form-item label="患者年龄" prop="patientAge">
      <el-input v-model="dataForm.patientAge" placeholder="患者年龄"></el-input>
    </el-form-item>
    <el-form-item label="问诊金额" prop="inquiryMoney">
      <el-input v-model="dataForm.inquiryMoney" placeholder="问诊金额"></el-input>
    </el-form-item>
    <el-form-item label="病情" prop="patientState">
      <el-input v-model="dataForm.patientState" placeholder="病情"></el-input>
    </el-form-item>
    <el-form-item label="问诊医生id" prop="docId">
      <el-input v-model="dataForm.docId" placeholder="问诊医生id"></el-input>
    </el-form-item>
    <el-form-item label="问诊医生姓名" prop="docName">
      <el-input v-model="dataForm.docName" placeholder="问诊医生姓名"></el-input>
    </el-form-item>
    <el-form-item label="问诊医生隶属医院" prop="hosName">
      <el-input v-model="dataForm.hosName" placeholder="问诊医生隶属医院"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          inquiryId: 0,
          patientId: '',
          createTime: '',
          patientName: '',
          patientAge: '',
          inquiryMoney: '',
          patientState: '',
          docId: '',
          docName: '',
          hosName: ''
        },
        dataRule: {
          patientId: [
            { required: true, message: '患者id不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '问诊日期不能为空', trigger: 'blur' }
          ],
          patientName: [
            { required: true, message: '患者姓名不能为空', trigger: 'blur' }
          ],
          patientAge: [
            { required: true, message: '患者年龄不能为空', trigger: 'blur' }
          ],
          inquiryMoney: [
            { required: true, message: '问诊金额不能为空', trigger: 'blur' }
          ],
          patientState: [
            { required: true, message: '病情不能为空', trigger: 'blur' }
          ],
          docId: [
            { required: true, message: '问诊医生id不能为空', trigger: 'blur' }
          ],
          docName: [
            { required: true, message: '问诊医生姓名不能为空', trigger: 'blur' }
          ],
          hosName: [
            { required: true, message: '问诊医生隶属医院不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.inquiryId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.inquiryId) {
            this.$http({
              url: this.$http.adornUrl(`/patient/inquiryrecord/info/${this.dataForm.inquiryId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.patientId = data.inquiryRecord.patientId
                this.dataForm.createTime = data.inquiryRecord.createTime
                this.dataForm.patientName = data.inquiryRecord.patientName
                this.dataForm.patientAge = data.inquiryRecord.patientAge
                this.dataForm.inquiryMoney = data.inquiryRecord.inquiryMoney
                this.dataForm.patientState = data.inquiryRecord.patientState
                this.dataForm.docId = data.inquiryRecord.docId
                this.dataForm.docName = data.inquiryRecord.docName
                this.dataForm.hosName = data.inquiryRecord.hosName
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/patient/inquiryrecord/${!this.dataForm.inquiryId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'inquiryId': this.dataForm.inquiryId || undefined,
                'patientId': this.dataForm.patientId,
                'createTime': this.dataForm.createTime,
                'patientName': this.dataForm.patientName,
                'patientAge': this.dataForm.patientAge,
                'inquiryMoney': this.dataForm.inquiryMoney,
                'patientState': this.dataForm.patientState,
                'docId': this.dataForm.docId,
                'docName': this.dataForm.docName,
                'hosName': this.dataForm.hosName
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
